package com.gitee.ice1938.module.admin.app.manage.fun;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.gitee.ice1938.module.admin.app.manage.appversiondetail.AppVersionDetailEntity;
import com.gitee.ice1938.module.admin.app.manage.appversiondetail.AppVersionDetailService;
import com.gitee.ice1938.module.admin.filemanager.fun.FileManageSvc;
import com.gitee.ice1938.module.admin.filemanager.fun.FileNode;
import com.gitee.ice1938.module.admin.filemanager.util.DownloadUtils;

/**
 * 版本更新接口
 */
@RestController
public class VersionController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = LoggerFactory.getLogger(VersionController.class);

	@Autowired
	private VersionService versionService;
	@Autowired
	private AppVersionDetailService appVersionDetailService;
	@Autowired
	private FileManageSvc fileManageSvc;

	/**
	 * @param version
	 * @param appPackage
	 * @return
	 */
	@RequestMapping(value = "/mobile/checkVersion", method = RequestMethod.POST)
	@ResponseBody
	public UpdateResult mobileCheckVersion(@RequestParam(value = "version") Integer version,
	        @RequestParam(value = "appKey") String appPackage) {
		UpdateResult result = versionService.checkVersion(version, appPackage);
		return result;
	}

	/**
	 * for https://github.com/WVector/AppUpdate
	 * 
	 * @param version
	 * @param appPackage
	 * @return
	 */
	@RequestMapping(value = "/com/mobile/checkVersion", method = RequestMethod.POST)
	@ResponseBody
	public AppUpdateResult checkVersion(@RequestParam(value = "version") Integer version,
	        @RequestParam(value = "appKey") String appPackage) {

		AppUpdateResult rm = new AppUpdateResult();
		try {
			UpdateResult result = versionService.checkVersion(version, appPackage);
			rm.setApk_url(result.getApkUrl());
			rm.setApk_file_url(result.getApkFileUrl());
			rm.setUpdate(result.getUpdateFlag() ? "Yes" : "No");
			rm.setUpdate_log(result.getVersionDesc());
			rm.setNew_version(String.valueOf(result.getVersionCode()));
			rm.setTarget_size(String.valueOf(result.getAppSize()));
			rm.setConstraint(result.getForce());
		} catch (Exception e) {
			logger.error("Integer, String", e);
			rm.setUpdate("No");
			rm.setStatus("error");
		}
		return rm;
	}

	/**
	 * for https://github.com/xuexiangjys/XUpdate
	 * 
	 * @param version
	 * @param appPackage
	 * @return
	 */
	@RequestMapping(value = "/update/checkVersion", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public XUpdateApiResult<XUpdateResult> xupdateCheckVersion(@RequestParam(value = "versionCode") Integer version,
	        @RequestParam(value = "appKey") String appPackage) {
		XUpdateApiResult<XUpdateResult> ret = new XUpdateApiResult<XUpdateResult>();
		XUpdateResult data = new XUpdateResult();
		try {
			UpdateResult result = versionService.checkVersion(version, appPackage);
			data.setApkMd5(result.getAppMd5());
			data.setVersionId(-1);
			data.setDownloadUrl(result.getApkFileUrl());
			data.setModifyContent(result.getVersionDesc());
			data.setUpdateStatus(result.getUpdateFlag() ? 1 : 0);
			data.setAppKey(appPackage);
			data.setVersionCode(result.getVersionCode());
			data.setVersionName(result.getVersionName());
			data.setApkSize(result.getAppSize());
			//data.setConstraint(result.getForce());
			data.setVersionId(result.getVersionId());

		} catch (Exception e) {
			data.setUpdateStatus(0);
		}
		ret.setData(data);
		return ret;
	}

	@RequestMapping(value = "/update/apk/{id}", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public void downFile(@PathVariable(value = "id") Integer id, HttpServletRequest request,
	        HttpServletResponse response) {
		AppVersionDetailEntity data = appVersionDetailService.findOne(id);
		FileNode fileInfo = fileManageSvc.readFile(data.getAppPath());
		DownloadUtils.download(fileInfo, request, response);
	}

}
